#!/usr/local/bin/python
import os
import re
from subprocess import *

f = file("import.dat")

group_name = None

for line in f.readlines():
    #print line.strip()
    if line.startswith("*"):
        r = re.search('(^\*GROUP\s+)(\w+)(\s+.+\s+)(\w*\n)', line)
        group_name = r.group(2).strip().lower()
        print "#"
        print "# %s" % r.group(2).strip()
        print "#"
        #group_name, etc = line.split(' ')
    elif line.startswith("-"):
        r = re.search('(-)(\w*)\s+(\w+)\s+(\d+)(.*)(\w*\n)', line)
        process_name = r.group(2).strip().lower()
        print "#"
        print "# %s %s" % (group_name.upper(), r.group(2).strip())
        print "#"
        process_filetype = r.group(3).strip().lower()
        process_mode_count = r.group(4).strip().lower()
        if process_mode_count == "0":
            process_mode_count = "None"
        process_description = r.group(5).strip()
        po = Popen("%s %s" % (group_name, process_name), shell = True, stdout = PIPE, stderr = PIPE)
        reason = po.stdout.readlines()
        for r in reason:
            print "# %s" % r,
        #print ("\t%s\t%s\t%s:%s") % (process_name, process_filetype, process_mode_count, process_description[2:])
        print ("cdp.%s.add_process(\"%s\", \"%s\", \"%s\", %s, \"%s\")" % (group_name, process_name, process_filetype, process_filetype, process_mode_count, process_description[2:]))
        if process_mode_count == "None":
            print ("params = []")
            print ("cdp.%s.%s.add_parameters(params)" % (group_name, process_name))


        #process_name, etc = line.split(' ')
    elif line.startswith("("):
        r = re.search("\((\d+?)\)(\s+.*)(\w*\n)", line)
        print ("params = []")
        print ("cdp.%s.%s.add_mode(%s, params, \"%s\")" % (group_name, process_name, r.group(1).strip(), r.group(2).strip()[:-2]))
    elif line.startswith("="):
        break
